package com.example.jn_dxs.Base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.multidex.MultiDex;
import androidx.multidex.MultiDexApplication;

import java.lang.ref.WeakReference;


public class BaseApplication extends MultiDexApplication implements Application.ActivityLifecycleCallbacks {

    public static WeakReference<Activity> activity;
    private String tempClass;

    public static BaseApplication baseApplication;
    private MediaPlayer mediaPlayer = new MediaPlayer();
    //    private MediaPlayer mediaPlayer;
    public static String head = "http://hbimg.huabanimg.com/f6ee1b095215b8c2955fd07e56e2739b2671cd3579f0d-5feCNB_fw658";

    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
        baseApplication = this;

//        EMOptions options = new EMOptions();
//        // 默认添加好友时，是不需要验证的，改成需要验证
//        options.setAcceptInvitationAlways(false);
//        EaseUI.getInstance().init(baseApplication, options);
//        EMClient.getInstance().init(this, options);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public static Context getContexe() {
        return baseApplication;
    }

    public static BaseApplication getInstance() {
        return baseApplication;
    }


    /**
     * token 过期
     */
    public void tokenExpire() {

    }

    /**
     * 获取系统提示音
     *
     * @return
     */
    public Uri getDefaultUri() {
        return RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION);

    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, Bundle bundle) {
        BaseApplication.activity = new WeakReference<>(activity);
        tempClass = activity.getClass().getSimpleName();
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        if (!activity.getClass().getSimpleName().equals(tempClass)) {
            BaseApplication.activity = new WeakReference<>(activity);
        }
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) {
    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
    }
}
